package com.tengfei.api.index;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tengfei.common.annotation.Log;
import com.tengfei.common.annotation.RepeatSubmit;
import com.tengfei.common.core.controller.BaseController;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.core.validate.AddGroup;
import com.tengfei.common.core.validate.EditGroup;
import com.tengfei.common.enums.BusinessType;
import com.tengfei.main.index.domain.bo.NoticeBo;
import com.tengfei.main.index.domain.vo.NoticeVo;
import com.tengfei.main.index.service.INoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 通知公告Controller
 *
 * @author tengfei
 * @date 2022-09-03
 */
@Validated
@Api(value = "通知公告控制器", tags = {"通知公告管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/index/notice")
public class NoticeController extends BaseController {

    private final INoticeService iNoticeService;

    /**
     * 查询通知公告列表
     */
    @ApiOperation("查询通知公告列表")
    @SaCheckPermission("index:notice:list")
    @GetMapping("/list")
    public TableDataInfo<NoticeVo> list(NoticeBo bo, PageQuery pageQuery) {
        return iNoticeService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取通知公告详细信息
     */
    @ApiOperation("获取通知公告详细信息")
    @SaCheckPermission("index:notice:query")
    @GetMapping("/{noId}")
    public R<NoticeVo> getInfo(@ApiParam("主键")
                               @NotNull(message = "主键不能为空")
                               @PathVariable("noId") Long noId) {
        return R.ok(iNoticeService.queryById(noId));
    }

    /**
     * 新增通知公告
     */
    @ApiOperation("新增通知公告")
    @SaCheckPermission("index:notice:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NoticeBo bo) {
        return toAjax(iNoticeService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改通知公告
     */
    @ApiOperation("修改通知公告")
    @SaCheckPermission("index:notice:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NoticeBo bo) {
        return toAjax(iNoticeService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除通知公告
     */
    @ApiOperation("删除通知公告")
    @SaCheckPermission("index:notice:remove")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    public R<Void> remove(@ApiParam("noId数组")
                          @NotEmpty(message = "noId不能为空")
                          @RequestParam("noIds") List<Long> noIds) {
        return toAjax(iNoticeService.deleteWithValidByIds(noIds) ? 1 : 0);
    }
}
